package main

import (
	"context"
	"flag"
	"log"
	"time"

	pb "gitee.com/rs1314/taiqiu/proto/dao"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

var (
	addr     = flag.String("addr", "localhost:60001", "the address to connect to")
	method   = flag.String("method", "AssetList", "AssetList/UserAssetList/UpdateUserAsset")
	userId   = flag.Int("userId", 1, "userId to update")
	teamId   = flag.Int("teamId", 1, "teamId to update")
	teamName = flag.String("teamName", "wudi", "TeamName")
	charType = flag.Int("charType", 1, "charType to update")
)

func main() {
	flag.Parse()
	// Set up a connection to the server.
	conn, err := grpc.Dial(*addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c := pb.NewXMetauDaoServiceClient(conn)

	// Contact the server and print out its response.
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	if *method == "CreateTeam" {
		req := &pb.CreateTeamReq{
			UserId:   uint64(*userId),
			TeamId:   uint64(*teamId),
			TeamName: *teamName,
			CharType: int32(*charType),
		}
		r, err := c.CreateTeam(ctx, req)
		if err != nil {
			log.Fatalf("query AssetList failed: %v", err)
		}
		log.Printf("CreateTeam ok %v", r)
	} else if *method == "UserTeam" {
		req := &pb.UserTeamReq{
			UserId: uint64(*userId),
		}
		r, err := c.GetUserTeam(ctx, req)
		if err != nil {
			log.Fatalf("query user failed: %v", err)
		}
		log.Printf("GetUserTeam ok %v", r)
	} else if *method == "JoinTeam" {
		req := &pb.JoinTeamReq{
			UserId:   uint64(*userId),
			TeamId:   uint64(*teamId),
			CharType: int32(*charType),
		}
		r, err := c.JoinTeam(ctx, req)
		if err != nil {
			log.Fatalf("query user failed: %v", err)
		}
		log.Printf("GetUserTeam ok %v", r)
	}
}
